﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Daybreak.Core;
using Daybreak.Core.Rendering;
using Daybreak.Core.SceneGraph;
using Daybreak.Core.Terrains;
using Daybreak.Core.Rendering.Materials;

namespace Tutorial2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Demo : Tutorials.TutorialApp
    {
        MipMapTerrain terrain;

        KeyboardState state;

        public Demo()
        {
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {

            base.LoadContent();

            scene.SceneInstance.CreateDefaultLighting();
            scene.SceneInstance.AmbientLight.Color = Color.DarkGray;
            scene.SceneInstance.DirectionalLight.SpecularColor = Color.Black;
            //scene.SceneInstance.AmbientLight.Color = Color.Red;

            CreateScene(scene); // scene 1 

            scene.Images.Load(false);

            xnaEngine.ShowScene(scene);

            xnaEngine.Camera = new MouseCamera(6);
            xnaEngine.Camera.Position = new Vector3(950, 250, 950);
            xnaEngine.Camera.RotationDegrees = new Vector2(30, 230);

            state = Keyboard.GetState();

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            KeyboardState newState = Keyboard.GetState();

            if (newState.IsKeyDown(Keys.Space) && state.IsKeyUp(Keys.Space))
                terrain.ShowBBoxes = !terrain.ShowBBoxes;

            state = newState;

            // TODO: Add your update logic here

            //base.Update(gameTime);
        }

        protected override void DrawFrame(GameTime gameTime)
        {
            WriteText("FPS " +  fpsCounter.Fps.ToString(), 10, 10, Color.Yellow);
            WriteText("Triangles " + terrain.TrinaglesCount.ToString(), 10, 20, Color.Yellow);

        }

        private void CreateScene(Scene scene)
        {
            // Patchsize must me (2^n)+1 
            int patchSize = 65;

            Scene.Image img1 = scene.CreateImage("texture1", "texture.jpg");

            Scene.Material mat1 = scene.CreateMaterial("default1", img1);
            Scene.Material mat2 = Scene.Material.Debug;

            HeightData data = new RawHeightData("heightmap.raw2");
            terrain = new MipMapTerrain(data, patchSize, MipMapTerrain.LodMode.GeoMipMap);
            terrain.Material = mat1;

            TerrainNode terraNode = new TerrainNode(terrain);

            scene.SceneInstance.AddRootNode(terraNode);



        }


    }
}
